import { initAppUpdate } from './app'

type AppInitType = 'onLaunch' | 'onShow' | 'onError' | 'onUnhandledRejection' | 'onPageNotFound'
interface IAppConfig {
  options?: App.LaunchShowOption | undefined
  updateApi?: () => Promise<any>
  update?: boolean
  isMoveToBack?: boolean
  isCloseSplashscreen?: boolean
}
export default function globalLifeCycle(type: AppInitType, { update, updateApi, isCloseSplashscreen = true, isMoveToBack = true }: IAppConfig = {}) {
  switch (type) {
    case 'onLaunch':
      // 初始化
      // #ifdef APP
      if (isMoveToBack) {
        moveToBack()
      }
      if (isCloseSplashscreen) {
        plus.navigator.closeSplashscreen()
      }
      // #endif
      if (update) {
        initAppUpdate(updateApi)
      }
      break
    default:
      break
  }
}

function moveToBack() {
  // 隐藏“再按一次退出应用”的提示，将应用隐藏至后台
  const main = plus.android.runtimeMainActivity()
  plus.runtime.quit = function () {
    // eslint-disable-next-line ts/ban-ts-comment
    // @ts-expect-error
    main.moveTaskToBack(false)
  }
}
